package com.aiyige.page.learn.behavior;

import android.content.Context;
import android.support.design.widget.CoordinatorLayout;
import android.util.AttributeSet;
import android.view.View;

import com.aiyige.R;

public class SearchBarBehavior extends CoordinatorLayout.Behavior<View> {
    View dependencyView;
    float maxDistance = 1;

    public SearchBarBehavior() {
    }

    public SearchBarBehavior(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean layoutDependsOn(CoordinatorLayout parent, View child, View dependency) {
        if (dependency.getId() == R.id.appbar) {
            dependencyView = dependency;
            return true;
        } else {
            return false;
        }
    }

    @Override
    public boolean onLayoutChild(CoordinatorLayout parent, View child, int layoutDirection) {
        maxDistance = dependencyView.getMeasuredHeight();
        return super.onLayoutChild(parent, child, layoutDirection);
    }

    @Override
    public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency) {
        float alpha = dependency.getBottom() / maxDistance;
        child.setAlpha(alpha);
        if (alpha < 0.1f) {
            child.setVisibility(View.INVISIBLE);
        } else {
            child.setVisibility(View.VISIBLE);
        }
        return false;
    }

}
